package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.userInfo.adapter.RecyclerStationAdapter;
import com.rongshu.view.userInfo.entity.RecyclerStationInfo;
import com.rongshu.view.userInfo.iview.RecyclerStationView;
import com.rongshu.view.userInfo.listener.OnRecyclerStationItemClickListener;
import com.rongshu.view.userInfo.presenter.RecyclerStationPresenter;
import com.rongshu.widget.BanyanDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class RecyclerStationActivity extends BaseActivity<RecyclerStationPresenter> implements RecyclerStationView,OnRecyclerStationItemClickListener {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.layout_empty)
    RelativeLayout layout_empty;

    //manager
    private LinearLayoutManager manager = null;
    //adapter
    private RecyclerStationAdapter adapter = null;
    //map
    private Map<String, Object> map = null;
    //intent
    private Intent intent = null;
    //phone
    private String phone = null;
    //删除文章dialog
    private BanyanDialog dialog = null;
    //delete contribute_id
    private int contribute_id = 0;
    //request
    private final int REQUEST_CODE = 0x01;

    @Override
    protected int initLayout() {
        return R.layout.activity_recycler_station_view;
    }

    @Override
    protected void initView() {
        //set presenter
        setPresenter(new RecyclerStationPresenter(this), this);
        //getIntent
        intent = getIntent();
        if (intent != null) {
            phone = intent.getStringExtra(Constant.INTENT_DATA);
        }
        //设置title
        txt_top_title.setText(R.string.recycler_station);
        //layoutmanager
        manager = new LinearLayoutManager(this);
        //adapter
        adapter = new RecyclerStationAdapter(this);
        adapter.setOnItemFunctionClickListener(this);
        //recyclerview
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(adapter);
        //触发自动刷新
        refreshLayout.autoRefresh();
        refreshLayout.setEnableLoadmore(false);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                getRecycleBinContribute();
                refreshlayout.finishRefresh(2000,false);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    /**
     * APP获取回收站文章名字列表
     */
    private void getRecycleBinContribute() {
        int user_id = UserInfoUtils.getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        presenter.getRecycleBinContribute(user_id, Common.encryptionSign(map));
        map.clear();
    }

    /**
     * APP恢复已删除的文章
     */
    private void getRecoverContribute(int contribute_id) {
        int user_id = UserInfoUtils.getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("contribute_id", contribute_id);
        presenter.getRecoverContribute(user_id, contribute_id, Common.encryptionSign(map));
        map.clear();
    }

    /**
     * APP彻底删除文章
     */
    private void getShiftDeleteContribute(int contribute_id) {
        int user_id = UserInfoUtils.getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("contribute_id", contribute_id);
        presenter.getShiftDeleteContribute(user_id, contribute_id, Common.encryptionSign(map));
        map.clear();
    }

    /**
     * 彻底删除dialog
     */
    private void showShiftDeleteDialog() {
        BanyanDialog.Builder builder = new BanyanDialog.Builder(this);
        builder.setTitle("彻底删除该文章");
        builder.setContent("确定此操作将无法逆转");
        builder.setLeft("取消");
        builder.setRight("确定");
        builder.setDialogOnClick(new BanyanDialog.Builder.onClickListener() {
            @Override
            public void leftClickListener() {
                dialog.dismiss();
            }

            @Override
            public void rightClickListener() {
                dialog.dismiss();
//                if (phone != null) {
//                    intent = new Intent(RecyclerStationActivity.this, SafetyVerificationActivity.class);
//                    intent.putExtra(Constant.INTENT_DATA, phone);
//                    startActivityForResult(intent, REQUEST_CODE);
//                } else {
//                    toast("没有绑定手机号");
//                }
                if (contribute_id == 0) {
                    return;
                }
                getShiftDeleteContribute(contribute_id);
            }
        });
        dialog = builder.create();
        dialog.show();
    }

    @OnClick({R.id.layout_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CODE:
                    if (contribute_id == 0) {
                        return;
                    }
                    getShiftDeleteContribute(contribute_id);
                    break;
            }
        }
    }

    @Override
    public void onItemClick(RecyclerStationInfo.ResponseBean responseBean) {

    }

    @Override
    public void onDeleteClick(int contribute_id) {
        this.contribute_id = contribute_id;
        showShiftDeleteDialog();
    }

    @Override
    public void onRecoverClick(int contribute_id) {
        getRecoverContribute(contribute_id);
    }

    /**
     * APP获取回收站文章名字列表
     *
     * @param info
     */
    @Override
    public void onRecyclerStationList(RecyclerStationInfo info) {
        if (info == null) {
            return;
        }
        List<RecyclerStationInfo.ResponseBean> responseBeans = info.getResponse();
        if (adapter != null) {
            refreshLayout.finishRefresh();
            if (responseBeans == null || responseBeans.isEmpty()) {
                refreshLayout.setVisibility(View.GONE);
                layout_empty.setVisibility(View.VISIBLE);
            } else {
                refreshLayout.setVisibility(View.VISIBLE);
                layout_empty.setVisibility(View.GONE);
                adapter.setRecyclerStationInfos(responseBeans);
                adapter.notifyDataSetChanged();
            }
        }
    }

    /**
     * APP恢复已删除的文章回调
     *
     * @param bean
     */
    @Override
    public void onRecoverContribute(INetBean bean) {
        toast(bean.getErrMsg());
        getRecycleBinContribute();
    }

    /**
     * APP彻底删除文章回调
     *
     * @param bean
     */
    @Override
    public void onShiftDeleteContribute(INetBean bean) {
        toast(bean.getErrMsg());
        getRecycleBinContribute();
    }
}
